package com.echo.yiwei.serivce;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.echo.util.GsonUtil;
import com.echo.util.RedisUtil;
import com.echo.util.StringEchoUtil;
import com.echo.util.jdbc.JdbcTempletEcho;
import com.google.gson.Gson;

public class UserService {
private  String userDataKeya="user:str:userId:[%s]";
private  static Logger logger= LoggerFactory.getLogger(UserService.class);


public    String  userSerivceBuiness(Map<String,String> requestMap){
	
		if(requestMap.get("method").indexOf("addUser")>=0){
			requestMap.remove("method");
	    	return 	 addUserData(requestMap);
	    	}
    	else	if(requestMap.get("method").indexOf("findUser")>=0){
    		requestMap.remove("method");
        	return  findUserData(requestMap);
        	}
    	else if(requestMap.get("method").indexOf("updateUser")>=0){
    		requestMap.remove("method");
        	return  updateUserData(requestMap);
        	}
    	else if(requestMap.get("method").indexOf("deleteUser")>=0){
    		requestMap.remove("method");
        	return  delUserData(requestMap);
        	}
    	
    	else{
    		requestMap.put("status", "999");
    		requestMap.put("mes", "对不起 我们不提供服务");
    		return new Gson().toJson(requestMap);
    	}
}





public String   addUserData(Map<String,String> map){
	 String userId=StringEchoUtil.getUUIdValue();
     map.put("userId", userId);
	String sql="INSERT INTO   user_info (id,NAME,nickname,login_number,phone)"
			+ "VALUES(#{userId},#{userName},#{nickName},#{loginNumber},#{phone})";
     boolean flag=  new JdbcTempletEcho().changeDataTemplet(sql, map, null);
   /*  if(flag){
       RedisUtil.getInstall().set(String.format(userDataKeya, userId),  GsonUtil.toJson(map));
     }*/
     map.clear();
      map.put("status",String.valueOf(flag));
      map.put("mes","保存成功");
     return GsonUtil.toJson(map);
}

public String  findUserData(Map<String,String> map){
          String userId=map.get("userId");
	       if(RedisUtil.getInstall().exists(String.format(userDataKeya, userId))){
			 String userData=  RedisUtil.getInstall().get(String.format(userDataKeya, userId));
			 logger.info("本次走缓存");
			  return userData;
	       }else{
	    	   String sql=" SELECT  id   userId,NAME userName,nickname nickName,login_number loginNumber ,phone phone ,DATE_FORMAT(last_modify_time,'%Y-%m-%d') lastTime   FROM user_info WHERE id=#{userId}";
	  	     Map<String, Object> resultMap = new JdbcTempletEcho().queryDateTempletForMap(sql, map, null);
	  	    String userData= GsonUtil.toJson(resultMap);
	  	       RedisUtil.getInstall().set(String.format(userDataKeya, userId),userData);
	  		   RedisUtil.getInstall().expireTime(String.format(userDataKeya, userId), 10);
	  		logger.info("------mysql select 查询----");
	  		  return userData;
	   
	    	   
	       }
	 

	       
	       
	       
}


public String  updateUserData(Map<String,String> map){
    
	String userId=map.get("userId");
	  
	  String userStr=   RedisUtil.getInstall().get(String.format(userDataKeya, userId));
	  Map<String,String> resultMap=  GsonUtil.fromJson(userStr,Map.class);
	     resultMap.putAll(map);
	  String status=  RedisUtil.getInstall().set(String.format(userDataKeya, userId),GsonUtil.toJson(resultMap));
	  logger.debug("更新信息 通过用户Id: {},数据库状态：{} ",userId,status);
	  return GsonUtil.toJson(resultMap);
}
public String  delUserData(Map<String,String> map){
		String userId = map.get("userId");
		long status = RedisUtil.getInstall().del(
				String.format(userDataKeya, userId));
		map.clear();
		map.put("stutus", String.valueOf(status));
		return GsonUtil.toJson(map);
}

}
